#!/usr/bin/python
# -*- coding: UTF-8 -*-
from colorama import init, Fore, Back, Style
from outexcel import write_excel
import mysql.connector.pooling
import yinxindb as YX
import time

init()

def say_hello():
    x = "XIAOYAO3"
    print(f'''
{"#"*30}
#{" "*28}#
#{" "*10}{x}{" "*10}#
#{" "*11}ver: 2{" "*11}#
#{" "*28}#
{"#"*30}

    {Fore.RED}1   银杏数据库(需要关闭银杏服务管理程序) {Style.RESET_ALL}
    {Fore.WHITE}0   结束 {Style.RESET_ALL}
    ''')

def execute_yinxin():
    """导出银杏数据"""
    print(f"{Fore.GREEN}\n...开始导出银杏数据...\n{Style.RESET_ALL}")
    try:
        pool = mysql.connector.pooling.MySQLConnectionPool(**YX.config)
    except BaseException as err:
        pool = None

    if not pool:
        print(f"[{Fore.YELLOW}Waiting{Style.RESET_ALL}] 检测数据服务状态")
        r = YX.is_service_exist(YX.sName)
        if not r:
            print(f"\r[{Fore.RED}False{Style.RESET_ALL}] 检测数据服务状态")
            return False
        else:
            print(f"\r[{Fore.GREEN}True{Style.RESET_ALL}] 检测数据服务状态")

        while True:
            r = YX.is_service_exist(YX.sName)
            if r == 4:
                print(f"[{Fore.YELLOW}Waiting{Style.RESET_ALL}] 停止数据服务")
                x = YX.do_service_stop(YX.sName)
                if not x:
                    print(f"\r[{Fore.RED}{x}{Style.RESET_ALL}] 停止数据服务")
                    return False
                else:
                    print(f"\r[{Fore.GREEN}{x}{Style.RESET_ALL}] 停止数据服务")
            if r == 1:
                break
            time.sleep(2)

        print(f"\r[{Fore.YELLOW}Waiting{Style.RESET_ALL}] 启动DEBUG模式")
        pid = YX.do_mysqldb_start(YX.sName)
        if not r:
            print(f"\r[{Fore.RED}{r}{Style.RESET_ALL}] 启动DEBUG模式")
            return False
        else:
            print(f"\r[{Fore.GREEN}{r}{Style.RESET_ALL}] 启动DEBUG模式")

        time.sleep(2)

        print(f"\r[{Fore.YELLOW}Waiting{Style.RESET_ALL}] 请求数据权限")
        r = YX.create_mysqldb_user()
        if not r:
            print(f"\r[{Fore.RED}{r}{Style.RESET_ALL}] 请求数据权限")
            return False
        else:
            print(f"\r[{Fore.GREEN}{r}{Style.RESET_ALL}] 请求数据权限")

        time.sleep(2)

        print(f"\r[{Fore.YELLOW}Waiting{Style.RESET_ALL}] 重启数据服务")
        r = YX.stop_process_by_pid(pid)
        time.sleep(2)
        r = YX.do_service_start(YX.sName)
        if not r:
            print(f"\r[{Fore.RED}{r}{Style.RESET_ALL}] 重启数据服务")
            return False
        else:
            print(f"\r[{Fore.GREEN}{r}{Style.RESET_ALL}] 重启数据服务")

        # 创建连接池
        pool = YX.mysql_connect()

    for i in range(10):
        print(f'\r{"."*i}')
        time.sleep(1)

    print(f"\r[{Fore.YELLOW}Waiting{Style.RESET_ALL}] 加载数据中")
    r = []
    for i in YX.list_shop(pool):
        y = {"name":i[0],"tag":i[1], "cprice":eval(str(i[2])),"sprice":eval(str(i[3])),"stock":i[4]}
        x = YX.read_sn(pool, i[5])
        y["code"]  = x[0] if x else None
        r.append(y)
        print(y)
        print("_"*20)
    write_excel(r)
    print(f"{Fore.GREEN}\n...导出银杏数据完成...\n{Style.RESET_ALL}")


if __name__ == '__main__':
    # YX.create_mysqldb_user()
    while True:
        say_hello()
        r = input("请输入序号：")
        if r == "1":
            execute_yinxin()
        if r == "0":
            print("程序结束")
            break